## Project: pyEnkido
## Module: StringTable
## Author: Salwan

# Read XML string table which include strings in both English and arabic languages
# in a EnkidoFont format.

import xml.dom.minidom
import string

# Languages
LANG_EN = 0
LANG_AR = 1

class StringTable(object):
    def __init__(self, filename, language = LANG_EN):
        self.stringTable = {}
        self.language = language
        try:
            string_dom = xml.dom.minidom.parse(filename)
            string_elements = string_dom.getElementsByTagName("string")
            if len(string_elements) > 0:
                for s in string_elements:
                    if s.hasAttribute("id"):
                        id = s.getAttribute("id")
                        if self.language == LANG_EN:
                            lang = s.getElementsByTagName("en")
                            if len(lang) <= 0:
                                raise "String element '%s' missing English item." % (id)
                            self.stringTable[id] = lang[0].childNodes[0].nodeValue
                        elif self.language == LANG_AR:
                            lang = s.getElementsByTagName("ar")
                            if len(lang) <= 0:
                                raise "String element '%s' missing Arabic item." % (id)
                            lst = []
                            for i in string.split(lang[0].childNodes[0].nodeValue, ','):
                                lst.append(int(i))
                            self.stringTable[id] = lst
                    else:
                        raise "There is a string element without an id attribute."
            else:
                raise "Could not parse string elements."
        except:
            raise